/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.cmict.data.form.api.service.impl;

import com.cmict.data.framework.common.pojo.PageParam;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.form.api.entity.SysCategory;
import com.cmict.data.form.api.mapper.SysCategoryMapper;
import com.cmict.data.form.api.service.SysCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 类别表
 *
 * @author pigx code generator
 * @date 2022-03-14 11:37:08
 */
@Service("sysCategoryService")
@RequiredArgsConstructor
@Slf4j
public class SysCategoryServiceImpl implements SysCategoryService {

    @Autowired
    SysCategoryMapper sysCategoryMapper;

    @Override
    public Boolean deleteSysCategory(Long id) {
        return sysCategoryMapper.deleteSysCategory(id);
    }

    @Override
    public SysCategory getSysCategory(Long id) {
        return sysCategoryMapper.selectById(id);
    }

    @Override
    public Long createSysCategory(SysCategory sysCategory) {
        if(null == sysCategory && null == sysCategory.getCategoryOrder() ){
            return 0L;
        }
        SysCategory returnCategory = sysCategoryMapper.selectOne(sysCategory.getCategoryOrder());
        if(null != returnCategory){
            sysCategoryMapper.updateCategoryOrder(sysCategory.getCategoryOrder());
        }
        sysCategoryMapper.insert(sysCategory);
        return sysCategory.getId();
    }

    @Override
    public void updateSysCategory(SysCategory sysCategory) {
        //传进来的排序字段为空直接更新
        if(null != sysCategory && null == sysCategory.getCategoryOrder()){
            sysCategoryMapper.updateById(sysCategory);
        }else if(null != sysCategory && null != sysCategory.getCategoryOrder()){
            //传进来的排序字段不为空
            SysCategory beiCategory = sysCategoryMapper.selectOne(sysCategory.getCategoryOrder());
            if(null != beiCategory && sysCategory.getId() != beiCategory.getId()){
                SysCategory yuanCategory = sysCategoryMapper.selectById(sysCategory.getId());
                Integer yuanCategoryOrder = yuanCategory.getCategoryOrder();
                Integer beiCategoryOrder = beiCategory.getCategoryOrder();
                //数据库中有相同的排序字段数据
                if(yuanCategoryOrder < beiCategoryOrder){
                    //小转大，前开后闭，中间值减1
                    sysCategoryMapper.littleToBig(yuanCategoryOrder,beiCategoryOrder);
                }else if(yuanCategoryOrder > beiCategoryOrder){
                    //大转小，前闭后开，中间值加1
                    sysCategoryMapper.bigToLittle(yuanCategoryOrder,beiCategoryOrder);
                }
                sysCategoryMapper.updateById(sysCategory);
            }else{
                //数据库中无相同的排序字段数据
                sysCategoryMapper.updateById(sysCategory);
            }
        }
    }

    @Override
    public List<SysCategory> getSimpleSysCategorys(Collection<Long> ids) {
        return sysCategoryMapper.selectBatchIds(ids);
    }

    @Override
    public List<SysCategory> getSysCategoryPage() {
        return sysCategoryMapper.selectList();
    }

    @Override
    public PageResult<SysCategory> getFormPage(PageParam pageParam) {
        return sysCategoryMapper.selectPage(pageParam,new QueryWrapper<SysCategory>().orderByAsc("category_order"));
    }

    @Override
    public List<SysCategory> getCategoryAll() {
        return sysCategoryMapper.getCategoryAll();
    }
}
